package com.yueya.business.bed.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;
import com.yueya.common.util.UUIDGenerator;

@SuppressWarnings("serial")
public class Room extends Model<Room> {
	
	private List<Bed> beds;
	public static Room dao = new Room();
	
	public boolean save(Room dao){
		dao.set("room_id", UUIDGenerator.getUUID());
		return dao.save();
	}
	
	public boolean update(Room dao){
		return dao.update();
	}
	
	public boolean remove(Room dao){
		dao.set("del_flag","1");
		return dao.update();
	}
	
	
	
	//栋编号
	public static  String no() {
			String number ="";
			String tag ="R";
			long count = 1 + Db
					.queryLong("SELECT COUNT(room_id)  FROM jg_room ");
			if (0 < count) {
				if (10 > count)
					number = tag + "000" + count;
				else if (100 > count)
					number = tag + "00" + count;
				else if (1000 > count)
					number = tag + "0" + count;
				else
					number = tag + count;
			}
		   
			return number;
	}
	
	 public List<Room> getList(String  floor_id) {
		 return Room.dao.find("select * from jg_room where floor_id=? order by room_no",floor_id);
	 }
	 
	 
	 
	public List<Map<String, Object>> getAllList(String floor_id) {
		    List<Map<String, Object>>  result = new ArrayList<Map<String, Object>>();
		    List<Room>  list = getList(floor_id);
		    Iterator<Room> it = list.iterator();
		    while(it.hasNext()) {
		    	Map<String, Object> map =new HashMap<String, Object>();
		    	Room room = it.next();
		    	map.put("id", room.getStr("room_id"));
		    	map.put("name", room.getStr("room_name"));
		    	map.put("config", room.getStr("room_config"));
		    	List<Map<String, Object>>listbed =	Bed.dao.getListAll(room.getStr("room_id"));
		    	map.put("type", listbed.size());
		    	map.put("beds", listbed);
		        result.add(map);
		     }	
		     
		 return result;
	 }

    
	public List<Record> getRoomTypeAndNum(){
		String sql ="select r.room_type roomtype, (select count(*) from jg_room jr where jr.room_type =r.room_type ) num  from jg_room r group by r.room_type";
		List<Record> list =Db.find(sql);
		return list;
	}

	public List<Bed> getBeds() {
		return beds;
	}

	public void setBeds(List<Bed> beds) {
		this.beds = beds;
	}
	
		
}
